﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Thrift.Protocol;
using Thrift.Transport;

namespace SearchInterface.helper
{
    static class ServiceConnector
    {
        public static hydrap2p.servicemanager.ServiceManager.Client GetServiceManager()
        {
            int port = Utilities.GetManagerPort();
            Thrift.Transport.TTransport transport = new Thrift.Transport.TSocket("localhost", port);
            transport.Open();
            return new hydrap2p.servicemanager.ServiceManager.Client(new Thrift.Protocol.TBinaryProtocol(transport));
        }

        public static hydrap2p.ServiceLocation getLocation(String servicename)
        {
            hydrap2p.servicemanager.ServiceManager.Client manager = GetServiceManager();
            if (manager == null)
                return null;

            hydrap2p.ServiceLocation location = null;
            try
            {
                location = manager.getServiceLocation(servicename);
            }
            catch (Exception)
            {
                location = null;
            }
            return location;
        }

        public static hydrap2p.logging.Logging.Client GetLogging()
        {
            hydrap2p.ServiceLocation location = getLocation("Logging");
            if (location == null)
                return null;

            TTransport transport;
            try
            {
                transport = new TSocket(location.Address, location.Port);
                transport.Open();
                return new hydrap2p.logging.Logging.Client(new TBinaryProtocol(transport));
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.StackTrace);
                return null;
            }
        }

        public static hydrap2p.library.Library.Client GetLibrary()
        {
            hydrap2p.ServiceLocation location = getLocation("Library");
            if (location == null)
                return null;

            TTransport transport;
            try
            {
                transport = new TSocket(location.Address, location.Port);
                transport.Open();
                return new hydrap2p.library.Library.Client(new TBinaryProtocol(transport));
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.StackTrace);
                return null;
            }
        }

        public static hydrap2p.download.Downloader.Client GetDownloader()
        {
            hydrap2p.ServiceLocation location = getLocation("Downloader");
            if (location == null)
                return null;

            TTransport transport;
            try
            {
                transport = new TSocket(location.Address, location.Port);
                transport.Open();
                return new hydrap2p.download.Downloader.Client(new TBinaryProtocol(transport));
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.StackTrace);
                return null;
            }
        }

        public static hydrap2p.search.Searcher.Client GetSearcher()
        {
            hydrap2p.ServiceLocation location = getLocation("Searcher");
            if (location == null)
                return null;

            TTransport transport;
            try
            {
                transport = new TSocket(location.Address, location.Port);
                transport.Open();
                return new hydrap2p.search.Searcher.Client(new TBinaryProtocol(transport));
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.StackTrace);
                return null;
            }
        }
	
    }
}
